import { parseTime } from "./index";

export async function creatMkdir() {
  const os = require("os");

  const basePath = `${os.userInfo().homedir}\\AppData\\Roaming`;

  const fs = require("fs");
  const path = require("path");

  const mlsk = path.join(basePath, "mlsk");
  try {
    const status1 = await fs.access(mlsk, fs.constants.F_OK, (err) => {
      if (err) {
        console.log("目录不存在");
        return Promise.resolve(false);
      } else {
        console.log("目录存在");
        return Promise.resolve(true);
      }
    });
    if (!status1) {
      const status2 = await fs.mkdir(mlsk, { recursive: true }, (err) => {
        if (err) {
          console.log("创建失败");
          return Promise.reject(err);
        } else {
          console.log("创建成功");
          return Promise.resolve();
        }
      });
    }
    const logs = path.join(mlsk, "logs");
    const status3 = await fs.access(logs, fs.constants.F_OK, (err) => {
      if (err) {
        console.log("目录不存在");
        return Promise.resolve(false);
      } else {
        console.log("目录存在");
        return Promise.resolve(true);
      }
    });
    if (!status3) {
      const status4 = await fs.mkdir(logs, { recursive: true }, (err) => {
        if (err) {
          console.log("创建失败");
          return Promise.reject(err);
        } else {
          console.log("创建成功");
          return Promise.resolve();
        }
      });
    }
    console.log("创建完毕");
  } catch (error) {
    console.log(error);
  }
}

export function logs(content) {
  const os = require("os");

  const basePath = `${os.userInfo().homedir}\\AppData\\Roaming\\mlsk\\logs`;

  const date = parseTime(new Date(), "{y}-{m}-{d}");

  const fs = require("fs");
  const path = require("path");

  const log = path.join(basePath, date);
  const time = parseTime(new Date(), "{y}-{m}-{d} {h}:{i}:{s}");

  const info = time + ": " + content + "\n";
  fs.appendFile(log + ".txt", info, (err) => {
    if (err) throw err;
    console.log(`日志生成成功${info}`);
  });
}